home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / Harvest C 1.3 / Source Code / CStringArray.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-12  |  2.9 KB  |  121 lines  |  [TEXT/KAHL]

  1. /******************************************************************************
  2.  CStringArray.c
  3.  
  4.         
  5.     SUPERCLASS = CArray
  6.     
  7.     Copyright © 1991 Symantec Corporation. All rights reserved.
  8.     
  9.  
  10.  ******************************************************************************/
  11.  
  12. #include "CStringArray.h"
  13. #include "stdlib.h"
  14. #include "Packages.h"
  15.  
  16.     
  17. Boolean            CStringArray::cCompAscending;    // compare for ascending sort if true
  18. CStringArray    *CStringArray::cCurrArray;        // array being sorted
  19.  
  20. /******************************************************************************
  21.  IStringArray
  22. ******************************************************************************/
  23.  
  24. void CStringArray::IStringArray( short maxStringLength)
  25. {
  26.     CArray::IArray( Min( maxStringLength, sizeof( Str255)));
  27.     
  28. }    /* CStringArray::IStringArray */
  29.  
  30. /******************************************************************************
  31.  IRes
  32. ******************************************************************************/
  33.  
  34. void CStringArray::IRes( short strListID, short maxStringLength)
  35. {
  36.     Handle    strList;
  37.     Str255    str;
  38.     short    i, numStrings;
  39.     
  40.     IArray( maxStringLength);
  41.     
  42.     /* ensure the STR# resource exists    */
  43.     
  44.     strList = GetResource( 'STR#', strListID);
  45.     FailNILRes( strList);
  46.     
  47.     /* resize once to accomodate all the strings    */
  48.     
  49.     numStrings = *(short*) *strList;
  50.     Resize( numStrings);
  51.     
  52.     for (i = 1; i < numStrings; i++)
  53.     {
  54.         GetIndString( str, strListID, i);
  55.         str[0] = Min( str[0], elementSize);
  56.         InsertAtIndex( str, i);
  57.     }
  58.  
  59. }    /* CStringArray::IRes */
  60.  
  61. /******************************************************************************
  62.  FindString
  63. ******************************************************************************/
  64.  
  65.     static int EqualStrings( void* str1, void* str2)
  66.     {
  67.         return IUEqualString( str1, str2);
  68.     }
  69.  
  70. long CStringArray::FindString( StringPtr targetString)
  71. {
  72.     cCompAscending = TRUE;
  73.  
  74.     return Search( targetString, EqualStrings);
  75.  
  76. }    /* CStringArray::FindString */
  77.  
  78. /******************************************************************************
  79.  Sort
  80. ******************************************************************************/
  81.  
  82.     void CStringArray::swap( size_t i, size_t j)
  83.     {
  84.         CStringArray::cCurrArray->Swap( i+1, j+1);
  85.     }
  86.     
  87.     int CStringArray::CompareStrings( size_t index1, size_t index2)
  88.     {
  89.         unsigned char *items = (unsigned char*) *CStringArray::cCurrArray->hItems;
  90.         
  91.         if (!cCompAscending)
  92.         {
  93.             long tmp = index1;
  94.             index1 = index2;
  95.             index2 = tmp;
  96.         }
  97.         return IUCompString( items + CStringArray::cCurrArray->ItemOffset( index1+1), 
  98.                         items + CStringArray::cCurrArray->ItemOffset( index2+1));
  99.     }
  100.  
  101.  
  102. void CStringArray::Sort( Boolean fAscending)
  103. {
  104.     SignedByte    state = HGetState( hItems);
  105.     
  106.         /* qsort won't move memory, but LoadSeg can        */
  107.         
  108.     cCompAscending = fAscending;
  109.     cCurrArray = this;
  110.     
  111.     HLock( hItems);
  112.     
  113.     _qsort( numItems, CompareStrings, swap);
  114.     
  115.     HSetState( hItems, state);
  116.     
  117.     cCurrArray = NULL;
  118.  
  119. }    /* CStringArray::Sort */
  120.  
  121.